package com.Lab427.workflow.service.DAGExecutor;

import com.Lab427.workflow.common.enums.NodeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.module.Configuration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import static com.Lab427.workflow.common.enums.NodeType.*;

/**
 * @author Yue
 * @date:2025/8/27 14.04
 * 节点类型与方法映射，
 */

@Service
public class NodeServiceDispatcher {

    @Autowired
    private  NodeService nodeService;

    // 存储「节点类型 -> NodeService方法」
    private final Map<NodeType, NodeFunction> dispatchMap = new HashMap<>();

    public NodeServiceDispatcher() {

        dispatchMap.put(IMPORT_WORD_DATA,(s,data)-> s.textOutput(data));
        dispatchMap.put(IMPORT_TEXT_DATA,(s,data)-> s.textOutput(data));
        dispatchMap.put(ANALYZE_DATA,(s,data)-> s.textOutput(data));
        dispatchMap.put(EXPORT_WORD_RESULT,(s,data)-> s.textOutput(data));
        dispatchMap.put(EXPORT_TEXT_RESULT,(s,data)-> s.analyseData(data));

    }

    public String execute(NodeType type, Map<String, Object> params) {
        NodeFunction  func = dispatchMap.get(type);
        if (func == null) {
            throw new IllegalArgumentException("未知节点类型: " + type);
        }
        return func.apply(nodeService,params);
    }
}
